const express = require("express")
// const jwt=require("jsonwebtoken")
let router = express.Router()


const query = require("../tools/db")
const { md, signtoken } = require("../config/config")



// 登录
router.post("/login", async (req, res, next) => {
    // 1、接参
    let { username, password } = req.body

    // 2、做判断
    if (!username) {
        next("请输入账号")
        return
    }
    if (!password) {
        next("请输入密码")
        return
    }

    // 3、查询数据库
    let sql = `select * from  e_member where username="${username}" and password="${md(password)}"`
    let [err, results] = await query(sql)
    if (err) {
        next("登录失败")
        return
    }

    if (results.length == 0) {
        next("用户名或密码错误")
        return
    }


    // 以上已经验证好了用户信息
    // 生成token，并将用户信息存储进去
    // console.log(results[0].mid);
    // let token=jwt.sign({ mid:results[0].mid },"哈哈0411",{expiresIn:60})
    let token = signtoken(results[0].mid)



    res.send({
        status: 200,
        msg: "登录成功",
        token
    })
})



module.exports = router